package com.ymy.thinking.in.spring.dependency.source;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.annotation.PostConstruct;

/**
 * ResolvableDependency 作为依赖来源
 * {@link ConfigurableListableBeanFactory#registerResolvableDependency(Class, Object)}
 * <p>
 * 1. ResolvableDependency 只能用于依赖注入, 不能用于依赖查找
 * 2. ResolvableDependency 只能用于类型的依赖注入, 不能用于名称的依赖注入
 *
 * @author Ringo
 * @date 2022/2/5 16:04
 */
public class ResolvableDependencySourceDemo {

    @Autowired
    private Person person;

    @PostConstruct
    public void init() {
        System.out.println(person);
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(ResolvableDependencySourceDemo.class);

        applicationContext.addBeanFactoryPostProcessor(beanFactory -> beanFactory.registerResolvableDependency(Person.class, new Person()));

        applicationContext.refresh();

        applicationContext.close();
    }

    private static class Person {
        private String name = "zs";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
